<template>
  <!-- tab切换进阶；后端获取数据返给前端-->
  <div class="box">
    <div class="menu">
      <ul>
        <li
          v-for="(item, index) in titles"
          :key="item.id"
          @click="show = index"
          :class="{ active: index === show, tab: true }"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>
    <div class="content">
      <div
        v-for="(item, index) in contents"
        :key="item.id"
        :class="{ selected: index === show, main: true }"
      >
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUpdated, onUnmounted } from "vue";
import axios, { Axios } from "axios";
// 定义空数组
let titles = ref([]);
let contents = ref([]);
// 记录单击内容的下标
const show = ref(0);

let fetchData = async () => {
  let { data } = await axios({
    method: "get",
    url: "http://192.168.218.1:5522/tabsContent",
  });
  // 解构赋值+重定义名
  let { titles: titlesDate, contents: contentDate } = data;
  // 后端数据赋值给前端定义的空数组
  titles.value = titlesDate;
  contents.value = contentDate;
};

// 挂载初始化执行fetchDate函数
onMounted(() => {
  fetchData();
});
</script>

<style  scoped>
* {
  padding: 0;
  margin: 0;
}
.box {
  width: 600px;
  height: 300px;
  border: 1px solid skyblue;
  margin: 50px auto;
}

.menu .tab {
  list-style-type: none;
  float: left;
  cursor: pointer;
  width: 200px;
  line-height: 50px;
  text-align: center;
  border: 1px solid #87ceeb;
  box-sizing: border-box;
}

.content .main {
  padding: 20px;
  display: none;
  text-indent: 20px;
  word-wrap: break-word;
}

.content .selected {
  display: block;
}

.active {
  background-color: pink;
}
</style>